<template>
  <div id="addSpacePage">
    <h2 style="margin-bottom: 16px; text-align: center">
      {{ route.query?.id ? '修改' : '创建' }} {{ SPACE_TYPE_MAP[spaceType] }}
    </h2>

    <!-- 空间信息表单 -->
    <a-form layout="vertical" :model="spaceForm" @finish="handleSubmit">
      <a-form-item label="空间名称">
        <a-input v-model:value="spaceForm.spaceName" placeholder="输入空间名称" allow-clear />
      </a-form-item>
      <a-form-item name="spaceLevel" label="空间级别">
        <a-select
          style="min-width: 180px"
          v-model:value="spaceForm.spaceLevel"
          placeholder="请选择空间级别"
          :options="SPACE_LEVEL_OPTIONS"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" :loading="loading" style="width: 100%"
          >提交</a-button
        >
      </a-form-item>
    </a-form>
    <!-- 空间级别介绍 -->
    <a-card title="空间级别介绍">
      <a-typography-paragraph>
        *目前仅开通普通版，如需升级空间，请联系QQ: 3346885032@qq.com
      </a-typography-paragraph>
      <a-typography-paragraph v-for="spaceLevel in spaceLevelList">
        {{ spaceLevel.text }} 大小: {{ formatBytes(spaceLevel.maxSize) }} 数量:
        {{ spaceLevel.maxCount }}
      </a-typography-paragraph>
    </a-card>
  </div>
</template>
<script lang="ts" setup>
import {
  updateSpaceUsingPost,
  getSpaceVoByIdUsingGet,
  addSpaceUsingPost,
  listSpaceLevelUsingGet,
} from '@/api/spaceController'
import {
  SPACE_LEVEL_MAP,
  SPACE_LEVEL_OPTIONS,
  SPACE_TYPE_ENUM,
  SPACE_TYPE_MAP,
} from '@/constants/space'
import { formatBytes } from '@/utils'
import { message } from 'ant-design-vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
const space = ref<API.SpaceVO>()
const spaceForm = reactive<API.SpaceUpdateRequest | API.SpaceEditRequest>({})

const router = useRouter()
const loading = ref(false)
const spaceLevelList = ref<API.SpaceLevel[]>([])
const spaceType = computed(() => {
  if (route.query?.type) {
    return Number(route.query?.type)
  } else {
    return SPACE_TYPE_ENUM.PRIVATE
  }
})

const fetchSpaceLevelList = async () => {
  const res = await listSpaceLevelUsingGet()
  if (res.data.code === 200) {
    spaceLevelList.value = res.data.data
  } else {
    message.error('获取空间级别失败：' + res.data.message)
  }
}

/**
 * 提交表单
 */
async function handleSubmit() {
  const spaceId = space.value?.id
  loading.value = true
  let res
  if (spaceId) {
    //更新
    res = await updateSpaceUsingPost({
      id: spaceId,
      ...spaceForm,
    })
  } else {
    //创建
    res = await addSpaceUsingPost({
      spaceType: spaceType.value,
      ...spaceForm,
    })
  }
  if (res.data.code === 200) {
    message.success('操作成功')
    // 跳转到空间详情页面
    router.push(`/space/${res.data.data}`)
  } else {
    message.error('操作失败：' + res.data.message)
  }

  loading.value = false
}

onMounted(() => {
  fetchSpaceLevelList()
  getOldSpace()
})
const route = useRoute()
//获取老数据
const getOldSpace = async () => {
  const spaceId = route.query?.id
  if (spaceId) {
    const res = await getSpaceVoByIdUsingGet({ id: spaceId })
    if (res.data.code === 200 && res.data.data) {
      const data = res.data.data
      space.value = data
      spaceForm.spaceName = data.spaceName
      spaceForm.spaceLevel = data.spaceLevel
    }
  }
}
</script>
<style scoped>
#addSpacePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>